<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户ID" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="用户类型：1-管理员用户，2-会员用户" prop="userType">
        <el-select v-model="formData.userType" placeholder="请选择用户类型：1-管理员用户，2-会员用户">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="兑换码使用日期" prop="codeUsageDate">
        <el-date-picker
          v-model="formData.codeUsageDate"
          type="date"
          value-format="x"
          placeholder="选择兑换码使用日期"
        />
      </el-form-item>
      <el-form-item label="兑换码ID" prop="codeId">
        <el-input v-model="formData.codeId" placeholder="请输入兑换码ID" />
      </el-form-item>
      <el-form-item label="兑换码" prop="code">
        <el-input v-model="formData.code" placeholder="请输入兑换码" />
      </el-form-item>
      <el-form-item label="使用记录ID，关联ai_user_assets_code_usage_log.id" prop="usageLogId">
        <el-input v-model="formData.usageLogId" placeholder="请输入使用记录ID，关联ai_user_assets_code_usage_log.id" />
      </el-form-item>
      <el-form-item label="3日留存：0-未留存，1-留存，NULL-未计算" prop="day3Retention">
        <el-radio-group v-model="formData.day3Retention">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="第3日登录时间" prop="day3LoginDate">
        <el-date-picker
          v-model="formData.day3LoginDate"
          type="date"
          value-format="x"
          placeholder="选择第3日登录时间"
        />
      </el-form-item>
      <el-form-item label="3日留存计算时间" prop="day3CalculatedDate">
        <el-date-picker
          v-model="formData.day3CalculatedDate"
          type="date"
          value-format="x"
          placeholder="选择3日留存计算时间"
        />
      </el-form-item>
      <el-form-item label="7日留存：0-未留存，1-留存，NULL-未计算" prop="day7Retention">
        <el-radio-group v-model="formData.day7Retention">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="第7日登录时间" prop="day7LoginDate">
        <el-date-picker
          v-model="formData.day7LoginDate"
          type="date"
          value-format="x"
          placeholder="选择第7日登录时间"
        />
      </el-form-item>
      <el-form-item label="7日留存计算时间" prop="day7CalculatedDate">
        <el-date-picker
          v-model="formData.day7CalculatedDate"
          type="date"
          value-format="x"
          placeholder="选择7日留存计算时间"
        />
      </el-form-item>
      <el-form-item label="15日留存：0-未留存，1-留存，NULL-未计算" prop="day15Retention">
        <el-radio-group v-model="formData.day15Retention">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="第15日登录时间" prop="day15LoginDate">
        <el-date-picker
          v-model="formData.day15LoginDate"
          type="date"
          value-format="x"
          placeholder="选择第15日登录时间"
        />
      </el-form-item>
      <el-form-item label="15日留存计算时间" prop="day15CalculatedDate">
        <el-date-picker
          v-model="formData.day15CalculatedDate"
          type="date"
          value-format="x"
          placeholder="选择15日留存计算时间"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLoginDate">
        <el-date-picker
          v-model="formData.lastLoginDate"
          type="date"
          value-format="x"
          placeholder="选择最后登录时间"
        />
      </el-form-item>
      <el-form-item label="总登录天数" prop="totalLoginDays">
        <el-input v-model="formData.totalLoginDays" placeholder="请输入总登录天数" />
      </el-form-item>
      <el-form-item label="连续登录天数" prop="consecutiveLoginDays">
        <el-input v-model="formData.consecutiveLoginDays" placeholder="请输入连续登录天数" />
      </el-form-item>
      <el-form-item label="留存状态：0-计算中，1-已完成，2-异常" prop="retentionStatus">
        <el-radio-group v-model="formData.retentionStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UserRetentionStatsApi, UserRetentionStatsVO } from '@/api/system/userretentionstats'

/** 用户留存率统计 表单 */
defineOptions({ name: 'UserRetentionStatsForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  username: undefined,
  userType: undefined,
  codeUsageDate: undefined,
  codeId: undefined,
  code: undefined,
  usageLogId: undefined,
  day3Retention: undefined,
  day3LoginDate: undefined,
  day3CalculatedDate: undefined,
  day7Retention: undefined,
  day7LoginDate: undefined,
  day7CalculatedDate: undefined,
  day15Retention: undefined,
  day15LoginDate: undefined,
  day15CalculatedDate: undefined,
  lastLoginDate: undefined,
  totalLoginDays: undefined,
  consecutiveLoginDays: undefined,
  retentionStatus: undefined,
})
const formRules = reactive({
  userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
  userType: [{ required: true, message: '用户类型：1-管理员用户，2-会员用户不能为空', trigger: 'change' }],
  codeUsageDate: [{ required: true, message: '兑换码使用日期不能为空', trigger: 'blur' }],
  codeId: [{ required: true, message: '兑换码ID不能为空', trigger: 'blur' }],
  code: [{ required: true, message: '兑换码不能为空', trigger: 'blur' }],
  usageLogId: [{ required: true, message: '使用记录ID，关联ai_user_assets_code_usage_log.id不能为空', trigger: 'blur' }],
  retentionStatus: [{ required: true, message: '留存状态：0-计算中，1-已完成，2-异常不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UserRetentionStatsApi.getUserRetentionStats(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UserRetentionStatsVO
    if (formType.value === 'create') {
      await UserRetentionStatsApi.createUserRetentionStats(data)
      message.success(t('common.createSuccess'))
    } else {
      await UserRetentionStatsApi.updateUserRetentionStats(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    username: undefined,
    userType: undefined,
    codeUsageDate: undefined,
    codeId: undefined,
    code: undefined,
    usageLogId: undefined,
    day3Retention: undefined,
    day3LoginDate: undefined,
    day3CalculatedDate: undefined,
    day7Retention: undefined,
    day7LoginDate: undefined,
    day7CalculatedDate: undefined,
    day15Retention: undefined,
    day15LoginDate: undefined,
    day15CalculatedDate: undefined,
    lastLoginDate: undefined,
    totalLoginDays: undefined,
    consecutiveLoginDays: undefined,
    retentionStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>